package com.example.demo.test;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.soundcloud.android.crop.Crop;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv);
    }
    private Uri uriFromCamera;

    public void takeCreame(View view) {
        uriFromCamera = Crop.getOutputMediaFileUri(Crop.MEDIA_TYPE_IMAGE);
        Crop.pickImageCamera(this, uriFromCamera);
    }
    public void takePic(View view){
        Crop.pickImage(this);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent result) {
        if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
            if (uriFromCamera != null)
                beginCrop(uriFromCamera);
        } else if (requestCode == Crop.REQUEST_CROP) {
            handleCrop(resultCode, result);
        }
    }

    private String fileName = "test.jpg";
    private String directory = "/Demo";
    private String path = Environment.getExternalStorageDirectory().toString();

    private void beginCrop(Uri source) {
        path = Environment.getExternalStorageDirectory().toString() + directory;
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        Uri destination = Uri.fromFile(new File(path, fileName));
        // Crop.of(source, destination).withMaxSize(1280,1280).start(this);
//        Crop.of(source, destination).asSquare().start(this);
        Crop.of(source, destination).asCircle().start(this);
    }

    private void handleCrop(int resultCode, Intent result) {
        if (resultCode == RESULT_OK) {
            iv.setImageURI(null);
            iv.setImageURI(Crop.getOutput(result));
        } else if (resultCode == Crop.RESULT_ERROR) {
            Toast.makeText(getApplicationContext(), "" + Crop.getError(result).getMessage(),Toast.LENGTH_SHORT);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}